import {PageContainer, ProForm, ProFormTextArea} from '@ant-design/pro-components';
import React, {useState} from 'react';
import {adminGetPrompt, adminSetPrompt, PromptInfo} from "@/services/family/api";
import to from "await-to-js";
import {message} from "antd";
import {FormLayout} from "antd/lib/form/Form";

const PromptSetting: React.FC = () => {
  const [formLayoutType, setFormLayoutType] = useState<FormLayout>('vertical');

  return (
    <PageContainer
      header={{title: null,}}
    >
      <ProForm<PromptInfo>
        layout={formLayoutType}
        rowProps={{
          gutter: [16, formLayoutType === 'inline' ? 16 : 0],
        }}
        onFinish={async (values) => {
          const sessionPrompt = values.sessionPrompt;
          const reportPrompt = values.reportPrompt;
          if (!sessionPrompt || !reportPrompt) {
            return message.error("报告提示词和会话提示词均不能为空");
          }

          // return await adminGetPrompt();
          const [err, res] = await to(adminSetPrompt({
            sessionPrompt, reportPrompt
          }));
          if (err || res?.code !== '0') {
            message.error('提示保存失败');
          }
          message.success('提示保存成功');
          return;
        }}
        request={async () => {
          // return await adminGetPrompt();
          const [err, res] = await to(adminGetPrompt());
          if (err || res?.code !== '0') {
            message.error('提示获取失败');
            return {};
          }
          return res.result;
        }}
        autoFocusFirstInput
      >
        <ProFormTextArea
          colProps={{ md: 12, xl: 12 }}
          // span={24}
          label={<b>新建会话提示词</b>}
          name="sessionPrompt"
          fieldProps={{
            autoSize: {minRows: 4, maxRows: 40},
          }}
        />
        <ProFormTextArea
          label={<b>报告生成提示词</b>}
          name="reportPrompt"
          colProps={{ md: 12, xl: 12 }}
          // span={24}
          fieldProps={{
            autoSize: {minRows: 4, maxRows: 30},
          }}
        />
      </ProForm>

    </PageContainer>
  );
};

export default PromptSetting;
